package com.sphd.restaurantsystem.activitys.marking;

import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.FoodtogoSettingPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.FoodtogoSettingView;
import com.wzh.frame.utils.StringUtils;

import org.litepal.crud.DataSupport;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FoodtogoSettingActivity extends BaseActivity implements FoodtogoSettingView {
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.sendfood_switch)
    Switch sendfoodSwitch;
    @BindView(R.id.sendfood_starttime_textview)
    TextView sendfoodStarttimeTextview;
    @BindView(R.id.sendfood_endtime_textview)
    TextView sendfoodEndtimeTextview;
    @BindView(R.id.time_layout)
    LinearLayout timeLayout;
    @BindView(R.id.sendfood_fanwei)
    MaterialEditText sendfoodFanwei;
    @BindView(R.id.sendfood_money_switch)
    Switch sendfoodMoneySwitch;
    @BindView(R.id.sendfood_price)
    MaterialEditText sendfoodPrice;
    @BindView(R.id.sendfood_explan)
    MaterialEditText sendfoodExplan;
    @BindView(R.id.sendfood_remark)
    MaterialEditText sendfoodRemark;
    @BindView(R.id.sendfood_linear)
    LinearLayout sendfoodLinear;
    @BindView(R.id.footogo_setting_update)
    Button footogoSettingUpdate;
    @BindView(R.id.content_foodtogo_setting)
    NestedScrollView contentFoodtogoSetting;
    private Intent intent;
    private Bundle bundle;
    private int mHour;
    private int mMinute;
    //定义显示时间控件
    private Calendar calendar; //通过Calendar获取系统时间
    private boolean is_send = false;
    private String sendStart = "00:00";
    private String sendEnd = "00:00";
    private String sendLimit = "sendLimit";
    private String price = "price";
    private String remark = "remark";
    FoodtogoSettingPresenterImpl foodtogoSettingPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foodtogo_setting);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> finish());
        sendfoodMoneySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sendfoodPrice.setVisibility(View.VISIBLE);

                } else {

                    sendfoodPrice.setVisibility(View.GONE);
                }
            }
        });
        sendfoodSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    is_send = isChecked;
                    sendfoodLinear.setVisibility(View.VISIBLE);
                } else {
                    is_send = isChecked;
                    sendfoodLinear.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        calendar = Calendar.getInstance();
        setTitleBar(toolbar,"送餐设置");
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        foodtogoSettingPresenter=new FoodtogoSettingPresenterImpl(this);
        foodtogoSettingPresenter.getResInfo(RestaurantUrl.Httptitle+ RestaurantUrl.RESTAURANT+ "cgid=" + restaurantInfo.getId());
    }

    @OnClick({R.id.time_layout, R.id.footogo_setting_update, R.id.sendfood_starttime_textview, R.id.sendfood_endtime_textview})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.time_layout:
                break;
            case R.id.footogo_setting_update:
                if (is_send){
                    if (getMessage()){
                        foodtogoSettingPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.ChangeSendfoodset
                                + "isOsale=" + 1 + "&sendStart=" + sendStart +
                                "&sendEnd=" + sendEnd +
                                "&sendLimit=" + sendLimit +
                                "&remark=" + StringUtils.utf8Encode(remark) +
                                "&cgid=" + restaurantInfo.getId() +
                                "&price=" + price);

                    }

                }else {
                    foodtogoSettingPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.ChangeSendfoodset
                            + "isOsale=" + 0 + "&sendStart=" +
                            "&sendEnd=" +
                            "&sendLimit=" +
                            "&remark=" +
                            "&cgid=" + restaurantInfo.getId() +
                            "&price=");
                }

                if(isCg_has_storemanager(restaurantInfo)&&isSuperManager(loginInfo)){
                    Toast.makeText(getApplicationContext(), "店长会去操作，您不用费心了", Toast.LENGTH_SHORT).show();
                }else {

                }

                break;
            case R.id.sendfood_starttime_textview:
                setTimeDialog(0);
                break;
            case R.id.sendfood_endtime_textview:
                setTimeDialog(1);
                break;
        }
    }

    private void setTimeDialog(int i) {
        new TimePickerDialog(context,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hour, int minute) {
                        // TODO Auto-generated method stub
                        mHour = hour;
                        mMinute = minute;
                        //更新EditText控件时间 小于10加0
                        StringBuilder stringBuilder = new StringBuilder()
                                .append(mHour < 10 ? "0" + mHour : mHour).append(":")
                                .append(mMinute < 10 ? "0" + mMinute : mMinute);
                        if (i == 0) {
                            sendfoodStarttimeTextview.setText(stringBuilder);
                        } else if (i == 1) {
                            sendfoodEndtimeTextview.setText(stringBuilder);

                        }

                    }
                }, calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), true).show();

    }

    private Boolean getMessage() {
        boolean isempty = false;
        if (!TextUtils.isEmpty(sendfoodRemark.getText())) {
            isempty = true;
            remark = sendfoodRemark.getText().toString().trim();
        } else {
            remark = "";
            Toast.makeText(context, "备注为空", Toast.LENGTH_SHORT).show();

        }

        if (!TextUtils.isEmpty(sendfoodExplan.getText())) {

            sendLimit = sendfoodExplan.getText().toString().trim();
            isempty = true;
        } else {
            isempty = false;
            Toast.makeText(context, "起送价格空", Toast.LENGTH_SHORT).show();
            return isempty;
        }
        if (sendfoodMoneySwitch.isChecked()) {
            if (!TextUtils.isEmpty(sendfoodPrice.getText())) {
                price = sendfoodPrice.getText().toString().trim();
            } else {
                price = "0";
            }
        } else {
            price = "0";
        }


        sendStart = sendfoodStarttimeTextview.getText().toString().trim();
        sendEnd = sendfoodEndtimeTextview.getText().toString().trim();

        return isempty;
    }

    @Override
    public void recMsg(RestaurantInfo restaurantInfo) {
        this.restaurantInfo=restaurantInfo;
        if (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo)) {
            footogoSettingUpdate.setEnabled(false);
        } else {
            footogoSettingUpdate.setEnabled(true);
        }
        is_send = restaurantInfo.isCg_isosale();
        sendfoodSwitch.setChecked(is_send);
        if (is_send) {
            sendfoodLinear.setVisibility(View.VISIBLE);
            sendfoodRemark.setText(restaurantInfo.getCg_remark());
            sendfoodStarttimeTextview.setText(restaurantInfo.getCg_sendst());
            sendfoodEndtimeTextview.setText(restaurantInfo.getCg_sendet());
            sendfoodExplan.setText(restaurantInfo.getCg_lowlimit() + "");
            if (restaurantInfo.getCg_shipping() > 0) {

                sendfoodMoneySwitch.setChecked(true);

                sendfoodPrice.setVisibility(View.VISIBLE);
            } else {
                sendfoodMoneySwitch.setChecked(false);
                sendfoodPrice.setVisibility(View.GONE);
            }
            sendfoodPrice.setText(restaurantInfo.getCg_shipping() + "");
        } else {
            sendfoodLinear.setVisibility(View.GONE);
        }

    }

    @Override
    public void recRes(int res) {
        if (res==1){
            toast("提交成功");
            finish();
        }else {
            toast("提交失败，请重试");
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
